export default {
  /*getters可以帮我们从state中进一步过滤我们需要的一些状态条件*/

  /*判断商品是否已经在购物车内*/
  hasGoods: state => goods => {
    return goods.allbusinessId in state.cart
  },
  hasSelected: state => goods => {
    return goods.allbusinessId in state.cartSelected
  },
  /*返回购物车的数量*/
  cartNum: state => {
    let cart = state.cart,
      cartPlus = 0
    Object.keys(cart).forEach(key => {
      cartPlus += cart[key].amount
    })
    return cartPlus
    // return Object.keys(cart).length
  },
  /*返回即将购买商品数量*/
  selectedCartNum: state => {
    let cartSelected = state.cartSelected
    return Object.keys(cartSelected).length
  },
  /*计算购物车的金额*/
  cartsMoney: state => {
    let cartSelected = state.cartSelected
    let money = 0
    for (let item in cartSelected) {
      money += cartSelected[item].price * cartSelected[item].amount
    }
    return Number(money)
  },
  // 购物车促销合并
  delProLists: state => key => {
    let cart = state[key]
    let cartSelected = state.cartSelected
    let redata = [],
      dealData = function(list) {
        let aa = '无促销'
        if (list.ecCoupons) {
          list.ecCoupons.forEach(item => {
            if (item.id == list.couponId) {
              aa = item.name
            }
          })
        } else {
          aa = '无促销'
        }
        return aa
      }
    for (let i in cart) {
      let reObj = {
        name: dealData(cart[i]),
        couponId: cart[i].couponId,
        details: [cart[i]],
        reduce: 0,
        total: 0
      }
      if (cart[i].couponId != '0') {
        redata.push(reObj)
      } else {
        redata.unshift(reObj)
      }
    }
    let newData = []
    let newObj = {}
    redata.forEach((el, i) => {
      // console.log(newObj[el.name], i)
      if (!newObj[el.couponId]) {
        newData.push(el)
        newObj[el.couponId] = true
      } else {
        // console.log('ooppp')
        newData.forEach(item => {
          if (item.couponId === redata[i].couponId) {
            item.details = item.details.concat(redata[i].details)
          }
        })
      }
    })
    // 促销逻辑
    newData.forEach(item => {
      item.details.forEach(itemI => {
        if (itemI.allbusinessId in cartSelected) {
          item.total += itemI.price * itemI.amount
        }
      })
      if (item.couponId === '0') {
        item.reduce = 0
      } else {
        item.details.forEach(itemP => {
          if (itemP.allbusinessId in cartSelected) {
            item.details[0].ecCoupons.forEach(itemN => {
              if (itemN.id === item.couponId) {
                if (itemN.type === '0') {
                  if (itemN.eachReduction === '1') {
                    item.reduce = (
                      Math.floor(item.total / itemN.fullReductionPrice) * itemN.price
                    ).toFixed(2)
                  } else if (itemN.eachReduction === '0') {
                    if (item.total >= itemN.fullReductionPrice) {
                      item.reduce = itemN.price.toFixed(2)
                    } else {
                      item.reduce = 0
                    }
                  }
                } else if (itemN.type === '1') {
                  item.reduce = ((item.total * (1 * 100 - itemN.discount * 100)) / 100).toFixed(2)
                }
              }
            })
          }
        })
      }
    })
    return newData
  }
}
